import request from '../utils/request'
import { getUrlSearch } from '../utils/token'
const token = getUrlSearch(window.location.href, 'token')
const customerServiceConfId = getUrlSearch(
  window.location.href,
  'customerServiceConfId'
)
const identityTypeCode = getUrlSearch(window.location.href, 'identityTypeCode')
const grantType = getUrlSearch(window.location.href, 'grantType') || 'wmxy'

let baseURL = ''

if (import.meta.env.MODE.trim() === 'development') {
  baseURL = '/apis/blade-auth'
} else {
  baseURL = '/blade-auth2'
}

const loginParam = {
  wmxy: 'Basic d214eTp3bXh5X3NlY3JldA==', // 完美校园
  supwisdom: 'Basic c3Vwd2lzZG9tOnN1cHdpc2RvbV9zZWNyZXQ=', // 树维
  dkv8: 'Basic ZGt2ODpka3Y4X3NlY3JldA==', // 迪科
  shdjxyCas: 'Basic c2hkanh5Q2FzOnNoZGp4eUNhc19zZWNyZXQ=' // 上海电机
}

// 登录接口
const loginData = {
  grant_type: grantType,
  scope: 'all',
  token,
  customerServiceConfId
}
if (identityTypeCode) loginData.identityTypeCode = identityTypeCode
export function login(showLoading = false) {
  return request({
    url: '/oauth/token',
    method: 'post',
    headers: {
      'Tenant-Id': '000000',
      Authorization: loginParam[grantType],
      'Role-Id': '1664528453134516226'
    },
    baseURL,
    data: loginData,
    showLoading
  })
}

// 初始化配置参数
export function getInitParam(data, showLoading = false) {
  // console.log('gadaata', data);
  return request({
    url: '/config/initParam',
    method: 'post',
    headers: {
      'Blade-Auth': 'bearer ' + window.access_token
    },
    data: {
      ...data,
      wxToken: token
    },
    showLoading
  })
}

// 获取背景列表
export function getBgList(showLoading = false) {
  return request({
    url: '/config/backImages',
    method: 'post',
    headers: {
      'Blade-Auth': 'bearer ' + window.access_token
    },
    data: {
      wxToken: token
    },
    showLoading
  })
}

// 设置背景图
export function setParam(data, showLoading = false) {
  return request({
    url: '/config/settingBackImage',
    method: 'post',
    headers: {
      'Blade-Auth': 'bearer ' + window.access_token
    },
    data: {
      ...data,
      wxToken: token
    },
    showLoading
  })
}

// 猜你想问   V1.1 弃用
export function getTop(data, showLoading = false) {
  return request({
    url: '/question/top',
    method: 'post',
    headers: {
      'Blade-Auth': 'bearer ' + window.access_token
    },
    data: {
      ...data,
      wxToken: token
    },
    showLoading
  })
}

// 回答问题
export function answer(data, showLoading = false) {
  return request({
    url: '/question/answer',
    method: 'post',
    headers: {
      'Blade-Auth': 'bearer ' + window.access_token
    },
    data: {
      ...data,
      wxToken: token
    },
    showLoading
  })
}

// 问题详情
export function detail(data, showLoading = false) {
  return request({
    url: '/question/detail',
    method: 'post',
    headers: {
      'Blade-Auth': 'bearer ' + window.access_token
    },
    data: {
      ...data,
      wxToken: token
    },
    showLoading
  })
}

// 历史记录列表
export function sessionLogList(data, showLoading = false) {
  return request({
    url: '/question/sessionLogList',
    method: 'post',
    headers: {
      'Blade-Auth': 'bearer ' + window.access_token
    },
    data: {
      ...data,
      wxToken: token
    },
    showLoading
  })
}

// 历史记录详情 V1.2 弃用
export function sessionLogDetailAbandon(data, showLoading = false) {
  return request({
    url: '/question/sessionDetailsList',
    method: 'post',
    headers: {
      'Blade-Auth': 'bearer ' + window.access_token
    },
    data: {
      ...data,
      wxToken: token
    },
    showLoading
  })
}
// 历史记录详情 esSessionLogApi/listBySessionId   get 请求   参数时sessionId
export function sessionLogDetail(id, showLoading = false) {
  return request({
    url: `/esSessionLogApi/listBySessionId?sessionId=${id}&wxToken=${token}`,
    method: 'get',
    headers: {
      'Blade-Auth': 'bearer ' + window.access_token
    },
    showLoading
  })
}

// 刷新热门问题
export function refreshTopList(data, showLoading = false) {
  return request({
    url: '/question/topList',
    method: 'post',
    headers: {
      'Blade-Auth': 'bearer ' + window.access_token
    },
    data: {
      ...data,
      wxToken: token
    },
    showLoading
  })
}

// 评价
export function evaluate(data, showLoading = false) {
  return request({
    url: '/question/evaluate',
    method: 'post',
    headers: {
      'Blade-Auth': 'bearer ' + window.access_token
    },
    data: {
      ...data,
      wxToken: token
    },
    showLoading
  })
}

// 前置状态
export function getTransportStatus(showLoading = false) {
  return request({
    url: '/config/getTransportStatus',
    method: 'post',
    headers: {
      'Blade-Auth': 'bearer ' + window.access_token
    },
    showLoading
  })
}

// 获取问题详情
export function getKnowledge(data, showLoading = false) {
  return request({
    url: '/question/getKnowledge',
    method: 'post',
    data,
    showLoading
  })
}

// 语音转文字
export function stt(data, showLoading = false) {
  return request({
    url: '/question/getSpeechRecognizerRESTful',
    method: 'post',
    data,
    showLoading,
    headers: {
      'Blade-Auth': 'bearer ' + window.access_token,
      'Content-Type': 'multipart/form-data'
    }
  })
}

// 上传图片
export function uploadImg(data) {
  return request({
    baseURL: '/digitPeopleUploadApi/blade-resource',
    url: '/oss/endpoint/put-file-attach?code=ali',
    method: 'post',
    data,
    headers: {
      'Blade-Auth': 'bearer ' + window.access_token,
      'Content-Type': 'multipart/form-data'
    }
  })
}

// 评价
export function newEvaluate(data, showLoading = false) {
  return request({
    url: '/question/evaluateApi',
    method: 'post',
    headers: {
      'Blade-Auth': 'bearer ' + window.access_token
    },
    data: {
      ...data,
      wxToken: token
    },
    showLoading
  })
}

// 历史记录分页
export function sessionLogPage(data, showLoading = false) {
  return request({
    url: `/question/sessionLogPage?sessionId=${data.sessionId}&current=${data.current}&size=${data.size}`,
    method: 'get',
    headers: {
      'Blade-Auth': 'bearer ' + window.access_token
    },
    showLoading
  })
}

// 历史记录分页(含标题的记录)
export function sessionLogPageNew(data, showLoading = false) {
  return request({
    url: `/question/sessionLogPageNew?sessionId=${data.sessionId}&current=${data.current}&size=${data.size}`,
    method: 'get',
    headers: {
      'Blade-Auth': 'bearer ' + window.access_token
    },
    showLoading
  })
}

// 查询所有对话记录
export function sessionLog(data, showLoading = false) {
  return request({
    url: `/esSessionLogApi/list?current=${data.current}&size=${data.size}`,
    method: 'get',
    timeout: 5000,
    headers: {
      'Blade-Auth': 'bearer ' + window.access_token
    },
    showLoading
  })
}
// 搜索对话记录
export function sessionLogSearch(data, showLoading = false) {
  return request({
    url: `/esSessionLogApi/list?current=${data.current}&size=${data.size}&ask=${data.content}`,
    method: 'get',
    headers: {
      'Blade-Auth': 'bearer ' + window.access_token
    },
    showLoading
  })
}

// 修改历史记录标题 v1.2弃用
export function updateSessionLogAbandon(data, showLoading = true) {
  return request({
    url: `/question/updateSessionLog?id=${data.id}&title=${data.title}`,
    method: 'get',
    headers: {
      'Blade-Auth': 'bearer ' + window.access_token
    },
    showLoading
  })
}
// 修改对话记录标题 esSessionLogApi/submitContent
export function updateSessionLog(data, showLoading = true) {
  return request({
    url: `/esSessionLogApi/submitContent?sessionId=${data.id}&ask=${data.title}`,
    method: 'get',
    headers: {
      'Blade-Auth': 'bearer ' + window.access_token
    },
    showLoading
  })
}
// 删除一条历史记录 v1.2废弃
export function deleteSessionLog(data, showLoading = true) {
  return request({
    url: `/question/deleteSessionLog?id=${data.id}`,
    method: 'get',
    headers: {
      'Blade-Auth': 'bearer ' + window.access_token
    },
    showLoading
  })
}

// 删除一条历史记录esSessionLogApi/deleteSessionLog  post 请求 sessionId
export function deleteOneSessionLog(params, showLoading = true) {
  console.log(`output->params`, params)
  return request({
    url: `/esSessionLogApi/deleteSessionLog`,
    method: 'post',
    headers: {
      'Blade-Auth': 'bearer ' + window.access_token
    },
    data: {
      sessionId: params,
      wxToken: token
    },
    showLoading
  })
}
// 搜索历史记录内容
export function seachSessionLogPageNew(data, showLoading = false) {
  return request({
    url: `/question/seachSessionLogPageNew?current=${data.current}&size=${data.size}&content=${data.content}`,
    method: 'get',
    headers: {
      'Blade-Auth': 'bearer ' + window.access_token
    },
    showLoading
  })
}

// 根据id查询标题
export function getSessionLogById(data, showLoading = false) {
  return request({
    url: `/question/getSessionLogById?id=${data.id}`,
    method: 'get',
    headers: {
      'Blade-Auth': 'bearer ' + window.access_token
    },
    showLoading
  })
}

// 获取智能体详情 /agent/detail/{id}
export function getAgentDetail(agentId, showLoading = false) {
  return request({
    url: `/agent/detail/${agentId}`,
    method: 'get',
    headers: {
      'Blade-Auth': 'bearer ' + window.access_token
    },
    showLoading
  })
}

// 获取智能体历史对话
export function getAgentHistoryDetail(appid, showLoading = false) {
  return request({
    url: `/agent/detail/installedAppId?installedAppId=${appid}`,
    method: 'get',
    headers: {
      'Blade-Auth': 'bearer ' + window.access_token
    },
    showLoading
  })
}

// 停止对话 question/modelStop
export function stopSession(data, showLoading = false) {
  return request({
    url: `/question/modelStop?taskId=${data.taskId}&appid=${data.appid}`,
    method: 'get',
    headers: {
      'Blade-Auth': 'bearer ' + window.access_token
    },
    showLoading
  })
}

// 试着问问 /question/tryAsking  messageId，installedAppId
export function tryAsking(data, showLoading = false) {
  return request({
    url: `/question/tryAsking?messageId=${data.msgid}&installedAppId=${data.appid}`,
    method: 'get',
    headers: {
      'Blade-Auth': 'bearer ' + window.access_token
    },
    showLoading
  })
}
// 获取字典 getBladeParams  get请求 key=evaluateType
export function getDict(keName, showLoading = false) {
  return request({
    url: `/question/getBladeParams?key=${keName}`,
    method: 'get',
    headers: {
      'Blade-Auth': 'bearer ' + window.access_token
    },
    showLoading
  })
}
